package com.webas.mixins;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.runtime.Component;

public class ShowComponentName {

    @Inject
    private ComponentResources resources;

    private String getComponentName() {
            Class< ? extends Component > containerClass = resources.getContainer().getClass();
            String componentName = containerClass.getSimpleName().toLowerCase();

            // return null for your top level components here
            if ( componentName.equals( "layout" ) ) {
                    return null;
            } else {
                    return containerClass.getSimpleName();
            }
    }

    void beginRender( MarkupWriter writer ) {
            String name = getComponentName();
            if ( name != null ) {
                    writer.element( "span", "style", "background-color:yellow" );
                    writer.writeRaw( "&nbsp;" + name + "(&nbsp;" );
                    writer.end();
            }
    }

    void afterRender( MarkupWriter writer ) {
            String name = getComponentName();
            if ( name != null ) {
                    writer.element( "span", "style", "background-color:yellow" );
                    writer.writeRaw( "&nbsp;" + name + ")&nbsp;" );
                    writer.end();
            }
    }

}